// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: 'test-qch4n'})
const db = cloud.database({env: 'test-qch4n'})
// 云函数入口函数
exports.main = async (event, context) => {
  let {status, page = 1, pages = 20} = event
  let data = {}
  if (status !== 999) {
    data = { status }
  }
  const {total = 0} = await db.collection('warnList').where(data).count()
  const batchTimes = Math.ceil(total / pages)
  if (page <= batchTimes) {
    // 等待所有
    try {
      let res = await db.collection('warnList')
      .where(data)
      .skip((page - 1) * pages)
      .limit(pages)
      .get()
      return {
        code: 0,
        data: res,
        page,
        pages,
        total
      }
    } catch (err) {
      console.log(err)
    }
  } else {
    return {
      code: 1
    }
  }  
}